361dc31d7bc4826d6e4d973d0c0d313a7c2bed8f,demos/Eclipse/AppMenuDemo/src/com/example/appmenudemo/Controls.java,AsyncHttpClientGet,doInBackground,#String#,13746
Before Change
//ref. http://jan.horneck.info/blog/androidhttpclientwithbasicauthentication
HttpEntity entity = null;
HttpParams httpParams = new BasicHttpParams();
int connection_Timeout = 5000;
HttpConnectionParams.setConnectionTimeout(httpParams, connection_Timeout);
HttpConnectionParams.setSoTimeout(httpParams, connection_Timeout);
/*ref. http://blog.leocad.io/basic-http-authentication-on-android/
String credentials = mUSERNAME + ":" + mPASSWORD;
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", "Basic " + base64EncodedCredentials);
client = new DefaultHttpClient();
*/
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
String strResult="";
try {
//AuthScope:
//host the host the credentials apply to. May be set to null if credenticals are applicable to any host.
//port the port the credentials apply to. May be set to negative value if credenticals are applicable to any port.
if (mAuthenticationMode != 0) {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(mHOSTNAME,mPORT), //
new UsernamePasswordCredentials(mUSERNAME, mPASSWORD));
}
HttpGet httpget = new HttpGet(stringUrl[0]);
//thanks to @renabor
if (mAuthenticationMode != 0) {
String _credentials = mUSERNAME + ":" + mPASSWORD;
String _base64EncodedCredentials = Base64.encodeToString(_credentials.getBytes(), Base64.NO_WRAP);
httpget.addHeader("Authorization", "Basic " + _base64EncodedCredentials);
}
//System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
this.publishProgress(statusCode);
strResult= "";
if (statusCode == 200) { //OK
entity = response.getEntity();
if (entity != null) {
strResult = EntityUtils.toString(entity);
}
}
} catch(Exception e){
e.printStackTrace();
}finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
return strResult;
}
@Override
After Change
@Override
protected String doInBackground(String... stringUrl) {
int status = HttpURLConnection.HTTP_NOT_FOUND;
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(stringUrl[0]);
mResponseCode = HttpURLConnection.HTTP_CREATED;
client3 = (HttpURLConnection)url.openConnection();
client3.setRequestMethod("GET");
if (mAuthenticationMode == 1) {
String _credentials = mUSERNAME + ":" + mPASSWORD;
String _base64EncodedCredentials = Base64.encodeToString(_credentials.getBytes(), Base64.NO_WRAP);
client3.setRequestProperty("Authorization", "Basic "+ _base64EncodedCredentials);
}
for (int i = 0; i < listHeaderName.size(); i++ ) {
client3.setRequestProperty(listHeaderName.get(i), listHeaderValue.get(i));
}
status = client3.getResponseCode();
mResponseCode = status;
this.publishProgress(status);
if (status == HttpURLConnection.HTTP_OK) { //OK
InputStream inputStream = client3.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
sb.append(inputLine);
}
inputStream.close();
}
else {
sb.append(String.valueOf(status));
}
client3.disconnect();
} catch (Exception e) {
return "";
}
return sb.toString();
}
@Override